package com.nuvsoft.android.nuvgamenet;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.UUID;

import android.app.Application;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.nuvsoft.android.nuvgamenet.net.ConnectionManager;
import com.nuvsoft.android.nuvgamenet.net.TCPHandler.TCPConnection;

public abstract class NuvGameNetApplication extends Application {
	private TCPConnection currentHost;
	public TCPConnection getCurrentHost() {
		return currentHost;
	}

	public void setCurrentHost(TCPConnection currentHost) {
		this.currentHost = currentHost;
	}

	private ConnectionManager connectionManager;
	private static String nick = UUID.randomUUID().toString();

	public ConnectionManager getConnectionManager() {
		return connectionManager;
	}

	public void onCreate() {
		super.onCreate();
		connectionManager = new ConnectionManager(this);
	}

	public static String getNick(){
		return nick;
	}
	
	protected static void setNick(String nick){
		NuvGameNetApplication.nick = nick;
	}
	
	public String getHardwareID() {
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}

	public abstract String getAppName();

	public String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e(NuvGameNetApplication.class.getSimpleName(), ex.toString());
		}
		return null;
	}
}
